package com.teacold.customdemo.recyclerdemo;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.Button;

import com.teacold.customdemo.R;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerActivity extends AppCompatActivity implements View.OnClickListener {
    private Button recycler_button;
    private RecyclerView recycler_recycler;
    private DiuffUtilsAdapter diuffUtilsAdapter;
    private LinkedHashSet<RecyclerBean> linkSet=new LinkedHashSet<>();
    private ArrayList<RecyclerBean> newData=new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler2);
        initView();
    }
    private void initView() {
        for (int i=0;i<50;i++){
            newData.add(new RecyclerBean(String.valueOf(i),"望眼欲穿"));
        }
        recycler_button=findViewById(R.id.recycler_button);
        recycler_recycler=findViewById(R.id.recycler_recycler);
        recycler_recycler.setLayoutManager(new LinearLayoutManager(this));
        diuffUtilsAdapter=new DiuffUtilsAdapter();
        diuffUtilsAdapter.setmData(newData);
        recycler_recycler.setAdapter(diuffUtilsAdapter);
        recycler_button.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.recycler_button:
                addData();
                break;
        }
    }

    private void addData() {
        for (int i = 45; i <60 ; i++) {
            newData.add(new RecyclerBean(String.valueOf(i),"望眼欲穿"));
        }
        linkSet.addAll(newData);
        newData.clear();
        newData.addAll(linkSet);
//        ArrayList<RecyclerBean> oldDatas=diuffUtilsAdapter.getmData();
        diuffUtilsAdapter.setmData(newData);
        diuffUtilsAdapter.notifyDataSetChanged();
//        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new RecyclerDiffCallback(oldDatas, newData), true);
//        diffResult.dispatchUpdatesTo(diuffUtilsAdapter);
    }
}
